
module.exports=class extends baseController{
    constructor(){
        super();
        this.ruleLogic=this.loadLogic('iframe/rule');
        this.router.post('/getItems',this.getItems.bind(this));
        this.router.post('/getItem',this.getItem.bind(this));
        this.router.post('/edit',this.edit.bind(this));
        this.router.post('/del',this.del.bind(this));
        this.router.post('/getItemsByPartnersID',this.getItemsByPartnersID.bind(this));
        return this.router;
    }

    static init(){
        return new this();
    }

    async getItems(req,res){
        let Result={};
        try{
            var FindFilterModel = {};
            var CurPage = req.body.CurPage ? Number(req.body.CurPage) : 1;
            var PageSize = req.body.PageSize ? Number(req.body.PageSize) : 20;
            var Sort = req.body.Sort ? req.body.Sort : "RuleID|desc";
            FindFilterModel.RuleName = req.body.RuleName;
            FindFilterModel.PartnersID=req.body.PartnersID? Number(req.body.PartnersID) : 0;
            FindFilterModel.CreateTime=req.body.CreateTime;
            FindFilterModel.RuleOrgID=req.body.RuleOrgID;
            let ret=await this.ruleLogic.getItems(FindFilterModel,CurPage,PageSize,Sort,req.headers.moduleid,req.user);
            Result=this.Success(1,ret);
        }catch(err){
            Result=this.Error('数据查询失败',err);
        }
        res.json(Result);
    }

    async getItem(req,res){
        let Result={};
        let ret={};
        try{
            var RuleID = req.body.RuleID ? Number(req.body.RuleID) : 0;
            ret = await this.ruleLogic.getItem(RuleID,req.user);
            if(RuleID>0){
                if(!ret.Model){
                    Result=this.Error('记录不存在');
                }
            }else{
                ret.Model={
                    RuleAble:[]
        };
    }


    Result=this.Success(1,ret);
        }catch(err){
            Result=this.Error('数据查询失败',err);
        }
        res.json(Result);
    }

    async edit(req,res){
        let Result={};
        try{
            let ret=await this.ruleLogic.insertOrUpdate(req.body,req.user);
            Result=this.Success(1,ret);
        }catch(err){
            Result=this.Error('数据查询失败',err);
        }
        res.json(Result);
    }

    async del(req,res){
        let Result={};
        try{
            let keys = req.body.keys;
            let ret=await this.ruleLogic.del(keys);
            Result=this.Success(1,ret);
        }catch(err){
            Result=this.Error('数据查询失败',err);
        }
        res.json(Result);
    }

    async getItemsByPartnersID(req,res){
        let Result={};
        try{
            let partnersId = req.body.partnersId || -1 ;
            let ret=await this.ruleLogic.getItemsByPartnersID(partnersId);
            Result=this.Success(1,ret);
        }catch(err){
            Result=this.Error('数据查询失败',err);
        }
        res.json(Result);
    }
}


